<?php
!defined('P_W') && exit('Forbidden');
InitGP(array('step'));
if (!$step) {
	$ajax_basename = EncodeUrl($_basename);
	$tags = $db->get_value('SELECT tags FROM pw_house_info WHERE hid='.pwEscape($hid));
	$tags = unserialize($tags);
	if (is_array($tags)) {
		$temp = array();
		$rs = $db->query('SELECT tagname FROM pw_tags WHERE tagid IN('.pwImplode($tags).')');
		while ($rt = $db->fetch_array($rs)) {
			$temp[] = $rt['tagname'];
		}
		$tags = implode(' ',$temp);
	} else {
		$tags = '';
	}
	require_once PrintMode('ajax_tags');
} else {
	InitGP(array('tags'));
	if (strlen($tags)>=100) Showmsg('标签长度过长');
	$tempTags = array_unique(explode(" ",preg_replace('/\s+/is',' ',trim($tags))));
	if (count($tempTags) > 5) {
		 Showmsg('最多只能关联5个标签');
	}
	if(!empty($tags)){
		foreach ($tempTags as $key => $value) {
			if (strlen($value)>15 || strlen($value)<3) {
				Showmsg('标签长度必须在3～15个字符');
			}
		}
	}
	$temp = array();
	foreach ($tempTags as $key => $value) {
		$rt = $db->get_one("SELECT tagid FROM pw_tags WHERE tagname=".pwEscape($value));
		if (!$rt) {
			$db->update("INSERT INTO pw_tags SET ".pwSqlSingle(array('tagname'=>$value,'num'=>0)));
			$temp[] = $db->insert_id();
		} else {
			$temp[] = $rt['tagid'];
		}
	}
	$db->update('UPDATE pw_house_info SET tags='.pwEscape(serialize($temp)).' WHERE hid='.pwEscape($hid));
	$pageCache = getPageCache('info',$hid);
	$pageCache->deleteCache('newThreadByTag');
	
	echo getLangInfo('msg','operate_success');
}

?>